/*************************************************************************
  > File Name: CompareCastAndMap.cpp
  > Author: 16hxliang3
  > Mail: 16hxliang3@stu.edu.cn
  > Created Time: Mon 14 Mar 2022 05:19:50 PM CST
 ************************************************************************/

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <unordered_map>
using namespace std;
clock_t start, eend;
struct A
{
  public:
    int num;
    int cha;
};
struct B
{
  public:
    int num;
};
int main()
{
    int times = 10000000;
    string a = "9";
    uint32_t b;
    double start, eend;
    start = clock(); //程序开始计时
    for (int i = 1; i < times; i++)
    {
        b = stoi(a, 0, 16);
    }
    eend = clock(); //程序结束用时
    double endtime = (double)(eend - start) / CLOCKS_PER_SEC;
    cout << "Total time:" << endtime << endl; // s为单位
    std::cout << b << std::endl;
    start = clock(); //程序开始计时
    for (int i = 1; i < times; i++)
    {
        static unordered_map<std::string, uint32_t> _map = {
            {"h0", 0}, {"h1", 1}, {"h2", 2},  {"h3", 3},  {"h4", 4},  {"h5", 5},  {"h6", 6},  {"h7", 7},
            {"h8", 8}, {"h9", 9}, {"ha", 10}, {"hb", 11}, {"hc", 12}, {"hd", 13}, {"he", 14}, {"hf", 15}};
        b = _map["h9"];
    };
    eend = clock(); //程序结束用时
    endtime = (double)(eend - start) / CLOCKS_PER_SEC;
    cout << "Total time:" << endtime << endl; // s为单位
    start = clock();                          //程序开始计时
    unordered_map<std::string, uint32_t> _map = {{"h0", 0},  {"h1", 1},  {"h2", 2},  {"h3", 3}, {"h4", 4},  {"h5", 5},
                                                 {"h6", 6},  {"h7", 7},  {"h8", 8},  {"h9", 9}, {"ha", 10}, {"hb", 11},
                                                 {"hc", 12}, {"hd", 13}, {"he", 14}, {"hf", 15}};
    for (int i = 1; i < times; i++)
    {
        b = _map["h9"];
    };
    eend = clock(); //程序结束用时
    endtime = (double)(eend - start) / CLOCKS_PER_SEC;
    cout << "Total time:" << endtime << endl; // s为单位
    start = clock();                          //程序开始计时
    for (int i = 1; i < times; i++)
    {
        b = _map["h9"];
    };
    eend = clock(); //程序结束用时
    endtime = (double)(eend - start) / CLOCKS_PER_SEC;
    cout << "Total time:" << endtime << endl; // s为单位
    std::cout << b << std::endl;
    return 0;
}
